package com.tutos.ui.student;

import java.util.Arrays;

import com.tutos.model.Estudiante;
import com.tutos.model.Genero;
import com.tutos.model.Monitor;
import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItem;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.data.validator.IntegerValidator;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.DateField;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.Form;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;

public class StudentEditForm extends Form {
	
	private static final String COMMON_FIELD_WIDTH = "12em";
   	private static final long serialVersionUID = 1L;    
    private GridLayout ourLayout;
    

    public StudentEditForm(BeanItem<Estudiante> estudianteItem) {
        setCaption("Editar Datos Personales");
                               
        ourLayout = new GridLayout(2, 5);
        ourLayout.setMargin(true, false, false, true);
        ourLayout.setSpacing(true);

        setLayout(ourLayout);

        // Set up buffering
        setWriteThrough(true); // we cant edit.
        setInvalidCommitted(false); // no invalid values in datamodel

        // FieldFactory for customizing the fields and adding validators
        setFormFieldFactory(new MonitorFieldFactory());
        setItemDataSource(estudianteItem); // bind to POJO via BeanItem
        
        // Determines which properties are shown, and in which order:
        setVisibleItemProperties(Arrays.asList(new String[] { 
                "fechaNacimiento", "telefono",
                "centroEducativo", "genero", "password"}));        	
    }

    /*
     * Override to get control over where fields are placed.
     */
    @Override
    protected void attachField(Object propertyId, Field field) {

        if (propertyId.equals("fechaNacimiento")) {
            ourLayout.addComponent(field, 0, 0);
        } else if (propertyId.equals("telefono")) {
            ourLayout.addComponent(field, 1, 0);        
        } else if (propertyId.equals("centroEducativo")) {
            ourLayout.addComponent(field, 0, 1);
        } else if (propertyId.equals("genero")) {
            ourLayout.addComponent(field, 1, 1);
        }  else if (propertyId.equals("password")) {
            ourLayout.addComponent(field, 0, 3);
        }
    }
	
    
    private class MonitorFieldFactory extends DefaultFieldFactory {
    	
    	private static final long serialVersionUID = 1L;
		
        public MonitorFieldFactory() {
        }

        @Override
        public Field createField(Item item, Object propertyId,
                Component uiContext) {
            Field f;
            ComboBox comboBox;        	          
                
                if ("password".equals(propertyId)) {
                    f = createPasswordField(propertyId);
                } else {
                    f = super.createField(item, propertyId, uiContext);
                }
                if ("fechaNacimiento".equals(propertyId)) {
                	DateField tf = (DateField) f;    
                    tf.setRequired(true);                    
                    tf.setRequiredError("Escoja su fecha de nacimiento");
                    tf.setWidth(COMMON_FIELD_WIDTH);
                } else if ("telefono".equals(propertyId)) {
                	TextField tf = (TextField) f;   
                    tf.setRequired(true);
                    tf.setRequiredError("Por favor ingrese el telefono");
                    tf.setInputPrompt("Teléfono");
                    tf.setWidth(COMMON_FIELD_WIDTH);
                    tf.addValidator(new IntegerValidator(
                            "El teléfono no debe contener caractéres"));
                } else if ("centroEducativo".equals(propertyId)) {
                    TextField tf = (TextField) f;
                    tf.setRequired(true);
                    tf.setRequiredError("Por favor ingrese su centro educativo");
                    tf.setInputPrompt("Centro educativo");
                    tf.setWidth(COMMON_FIELD_WIDTH);
                    tf.addValidator(new StringLengthValidator(
                            "El centro educativo debe contener entre 5 y 40 caracteres", 3, 40, false));                   
                } else if("genero".equals(propertyId)){
                	comboBox = new ComboBox();
                	comboBox.setCaption("Género");
                	comboBox.setPropertyDataSource(item.getItemProperty(propertyId));

                	BeanItemContainer<Genero> container = new BeanItemContainer<Genero>(Genero.class);
                	container.addBean(Genero.MASCULINO);
                	container.addBean(Genero.FEMENINO);
                	
                	comboBox.setContainerDataSource(container);
                	comboBox.setNullSelectionAllowed(false);
                	comboBox.select(Genero.MASCULINO);
                	comboBox.setTextInputAllowed(false);
                	return comboBox;
                }           	
            	            	           
            return f;
        }
        
        private PasswordField createPasswordField(Object propertyId) {
            PasswordField pf = new PasswordField();
            pf.setCaption(DefaultFieldFactory
                    .createCaptionByPropertyId(propertyId));
            pf.setNullRepresentation("");
            return pf;
        }

}    		

}
